package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.xty.Document;
import com.xty.Elements;
import com.xty.Xsoup;


public class Utils {
	public static String EncodeString(String str)
	{
		if(str != null)
		{
			try {
				str = URLEncoder.encode(str,"utf-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return str;
	}
	public static Document Translate(String urls,String language)
	{
		Document doc=null;
		
		try {
			doc = Xsoup.parse(Utils.GetHtml("http://translate-hatforrent.appspot.com/translate?language="+language+"&url="+urls, "utf-8"));
			
			doc.select(".google-src-text").remove();
	      
		} catch (Exception e) {
			e.printStackTrace();
		}

		return doc;
	}
	
	public static String Spin(String string)
	{
		String message;
		try {
			message = URLEncoder.encode(string, "UTF-8");
			URL url = new URL("http://freearticlerewriter.com/index.php");
	        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
	        connection.setDoOutput(true);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 AlexaToolbar/alxf-2.14 AlexaToolbar/pXVUnvuf-1.2 Firefox/3.6.21");
			connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
			connection.setRequestProperty("Accept-Charset", "	ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setRequestProperty("Keep-Alive", "115");
			connection.setRequestProperty("Cookie", "PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES");
			connection.setRequestProperty("Cache-Control", "max-age=0");
			OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
	        writer.write("article=" + message);
	        writer.write("&submit=Rewrite+Article");
	        writer.close();
	        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        	String content = "";			
	        	BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8")));
				while(1==1)
				{
					String str = reader.readLine();
					if(str==null) break;
					content+=str;				
				}
				Document doc = Xsoup.parse(content);
				string = doc.select("textarea").get(1).text();
	        }
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return string;
	}
	
	
	public static ArrayList<String> returnTag(String str){
		ArrayList<String> term = new ArrayList<String>();
		
		try {	        	
			URL url = new URL("http://webmasterviet.com/tag.php");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(10000);
			connection.setConnectTimeout(10000);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());

            writer.write("str="+str.replaceAll("[\\W]+", " "));
            writer.close();
            Document doc = null;
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	           	 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	           	 String content2 = "";
	           	 char[] buf = new char[102102];
				 int numRead = 0;
		         while((numRead=reader.read(buf)) != -1){
		            String readData = String.valueOf(buf, 0, numRead);
		            content2 += readData;
		            buf = new char[102102];
		         }
		         reader.close();
	           	 doc = Xsoup.parse(content2);
            }
        	
            Elements nou = doc.select(".tag");
            
            if(nou.size() > 0)
            {
            	for(int i = 0;i<nou.size();i++)
            	{
		            if(!term.contains(nou.get(i).text().toLowerCase())&& nou.get(i).text().length()>=5 && term.size()<10) {
		            	term.add(nou.get(i).text().toLowerCase());
		            }
		            if(i==10)
		            	break;
            	}
            }
            
        } catch (Exception e) {
			e.printStackTrace();
		}
		
		return term;
		
	}
	public static String md5(String str) throws NoSuchAlgorithmException {
		MessageDigest md;

		md = MessageDigest.getInstance("MD5");
		md.update(str.getBytes());

		byte byteData[] = md.digest();

		// convert the byte to hex format method 1
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < byteData.length; i++) {
			sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16)
					.substring(1));
		}

		str = sb.toString();
		return str;
	}
	public static String fomatDmain(String url)
	{
		if (url != null)
			url = url.toLowerCase().replaceAll("(http|https)+://(www.)*", "")
					.replaceAll("^[\\w\\W]+[\\/]{2}", "")
					.replaceAll("[\\/]+[\\w\\W]*$", "").replaceAll("www.", "")
					.replaceAll("[\\?]+[\\w\\W]*$", "")
					.replaceAll("[ !@#$%^&*?()\\[\\]{}+=,:;|\\\'\\\"/\\\\`~]+", "")
					.trim();

		return url;
	}
	public static String fomatDmains(String url)
	{
		if (url != null)
			url = url.toLowerCase().replaceAll("(http|https)+://", "")
					.replaceAll("^[\\w\\W]+[\\/]{2}", "")
					.replaceAll("[\\/]+[\\w\\W]*$", "")
					.replaceAll("[\\?]+[\\w\\W]*$", "")
					.replaceAll("[ !@#$%^&*?()\\[\\]{}+=,:;|\\\'\\\"/\\\\`~]+", "")
					.trim();

		return url;
	}
	public static String fomatSitemap(String url)
	{
		if (url != null)
		{
			if(!Pattern.matches("^http://[\\w\\W]+$",url)&&!Pattern.matches("^https://[\\w\\W]+$",url))
			{
				url = "http://"+url.toLowerCase().replaceAll("(http|https)+://", "").trim().replaceAll("[/]+$", "");
			}
		}
		return url;
	}
	public static void checkLoginTrue(HttpServletRequest request,HttpServletResponse response)
	{
		HttpSession session = request.getSession();
		if(session.getAttribute("users")!=null)
		{
			try {
				response.sendRedirect("/add-link");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void checkLoginFalse(HttpServletRequest request,HttpServletResponse response)
	{
		HttpSession session = request.getSession();
		if(session.getAttribute("users")==null)
		{
			try {
				response.sendRedirect("/login");
				return;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static String cleanContent(String str) {
		str = str.replaceAll(
				"(class|id)[ ]*[=]{1}[ ]*[\\'\\\"]{1}[^\\'\\\"]+[\\'\\\"]{1}",
				"");
		str = str.replaceAll("<script[^<]+</script[ ]*>", "").replaceAll("<p>[ ]*</[ ]*p>", "").replaceAll("<p>&nbsp;</[ ]*p>", "");
		return str;
	}
	public static String GetHtml(String link,String charset){
		String content="";
		
		URL url;
		try {
			url = new URL(link);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setReadTimeout(10000);
		    connection.setInstanceFollowRedirects(true);
		    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.21) Gecko/20110830 AlexaToolbar/alxf-2.14 AlexaToolbar/pXVUnvuf-1.2 Firefox/3.6.21");
			connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
			connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
			connection.setRequestProperty("Accept-Charset", "	ISO-8859-1,utf-8;q=0.7,*;q=0.7");
			connection.setRequestProperty("Connection", "keep-alive");
			connection.setRequestProperty("Keep-Alive", "115");
			connection.setRequestProperty("Cookie", "PREF=ID=987bb7a56b1cc7bc:U=691fa246d56f502e:FF=0:TM=1314876638:LM=1314876681:S=yBXzqp_WQq9ykX-j; NID=50=nfNXg7ymSSsfdzMnkEuo7t1GCgIEwcK5xmpFhu1mQWwvNI5nFHLZpRsy_1XnaoKrdj9hnz7za9-M2OIO99phfFSTmXln-xCvqzJaRWHsN0EQKgpmkvgUDTWPVF4I4c9L; SID=DQAAALcAAADhqr-J8Ls3CKhmbRjwTMMP1DjmIvR8qBFxv99saTaFFgoRsVf18W4p_BTocYoxYNMvsvh9sfjMj7KqLyd_s9Y0lwYY_Rrljd7t5IpKBTWWnsv9VSJ_qelH943zV8YIT3uuIdJbHhFlCnVEu_YealZoldWjf4VirYR0PIrr0i4_JU2JCHRmpyW0ACt3KcyLkukJlGDyT5yC5js3CBSTAhnWSylBpMWPWyWJzqXe2BQFcdfuU_qo00pAR-uEcMQ437M; HSID=A9Z-j3CKhzDEykAES");
			connection.setRequestProperty("Cache-Control", "max-age=0");
			BufferedReader reader=null;
			if("gzip".equals(connection.getContentEncoding()))
		    {
				reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(connection.getInputStream()), Charset.forName(charset)));
		    } else {
		    	reader= new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName(charset)));
		    }
			char[] buf = new char[102102];
			int numRead=0;
	        while((numRead=reader.read(buf)) != -1){
	            String readData = String.valueOf(buf, 0, numRead);
	            content += readData;
	            buf = new char[102102];
	        }
	        reader.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return content;
	}
	public static String fomatLink(String link,String sitemap)
	{
		String domain = link.replaceAll("[^\\/]+$", "");
		String para = link.replace(domain, "").replace(" ", "%20");
		
		try {
			para = URLEncoder.encode(para,"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		link = domain+para;

		link = link.replace("%3F", "?").replace(" ", "%20").replace("%25", "%");
		
		if(!Pattern.matches("^http://[\\w\\W]+$",link)&&!Pattern.matches("^https://[\\w\\W]+$",link))
		{
			if(Pattern.matches("^/[\\w\\W]+$",link))
			{
				link = "http://"+Utils.fomatDmains(sitemap)+link.trim().replaceAll("^(\\.\\./)+", "");
			} else {
				if(Pattern.matches("http://[^\\/]+/[\\w\\W]*", sitemap))
					link = sitemap.replaceAll("[^\\/]+$", "")+link.trim().replaceAll("^(\\.\\./)+", "");
				else
					link = sitemap+"/"+link.trim().replaceAll("^(\\.\\./)+", "");
			}
		}
		link = link.replace("%3D", "=");
		return link;
	}
	
	public static ArrayList<String> getStachSite()
	{
		ArrayList<String> site = new ArrayList<String>();
		site.add("stackoverflow.com");
		site.add("stackapps.com");
		site.add("careers.stackoverflow.com");
		site.add("serverfault.com");
		site.add("superuser.com");
		site.add("meta.stackoverflow.com");
		site.add("area51.stackexchange.com");
		site.add("webapps.stackexchange.com");
		site.add("gaming.stackexchange.com");
		site.add("webmasters.stackexchange.com");
		site.add("cooking.stackexchange.com");
		site.add("gamedev.stackexchange.com");
		site.add("math.stackexchange.com");
		site.add("photo.stackexchange.com");
		site.add("stats.stackexchange.com");
		site.add("tex.stackexchange.com");
		site.add("english.stackexchange.com");
		site.add("cstheory.stackexchange.com");
		site.add("programmers.stackexchange.com");
		site.add("unix.stackexchange.com");
		site.add("apple.stackexchange.com");
		site.add("wordpress.stackexchange.com");
		site.add("physics.stackexchange.com");
		site.add("diy.stackexchange.com");
		site.add("gis.stackexchange.com");
		site.add("electronics.stackexchange.com");
		site.add("android.stackexchange.com");
		site.add("security.stackexchange.com");
		site.add("bicycles.stackexchange.com");
		site.add("dba.stackexchange.com");
		site.add("drupal.stackexchange.com");
		site.add("sharepoint.stackexchange.com");
		site.add("scifi.stackexchange.com");
		site.add("ux.stackexchange.com");
		site.add("skeptics.stackexchange.com");
		site.add("rpg.stackexchange.com");
		site.add("judaism.stackexchange.com");
		site.add("mathematica.stackexchange.com");
		site.add("money.stackexchange.com");
		
		return site;
	}
	
	public static String convertNCR(String str)
	{
		final String[] NCRs = {"&#x", "&#", "\\u", "U+", "#x", "#"};
        StringBuffer result = new StringBuffer();
        
        for (int i = 0; i < NCRs.length; i++) {
            int radix;
            int foundIndex;
            int startIndex = 0;        
            final int STR_LENGTH = str.length();
            final String NCR = NCRs[i]; 
            final int NCR_LENGTH = NCR.length();
            
            if (NCR == "&#" || NCR == "#") {
                radix = 10; 
            } else {
                radix = 16;
            }
                                  
            while (startIndex < STR_LENGTH) {
                foundIndex = str.indexOf(NCR, startIndex);

                if (foundIndex == -1) {
                    result.append(str.substring(startIndex));
                    break;
                }

                result.append(str.substring(startIndex, foundIndex));
                if (NCR == "\\u" || NCR == "U+") {
                    startIndex = foundIndex + 6;
                    if (startIndex > str.length()) startIndex = -1; // for invalid Unicode escape sequences
                } else {
                    startIndex = str.indexOf(";", foundIndex);
                }

                if (startIndex == -1) {
                    result.append(str.substring(foundIndex));
                    break;
                }

                String tok = str.substring(foundIndex + NCR_LENGTH, startIndex);

                try {
                    result.append((char) Integer.parseInt(tok, radix));
                } catch (NumberFormatException nfe) {
                    try {
                        if (NCR == "\\u" || NCR == "U+") {
                            result.append(NCR + tok);
                        } else {
                            result.append(NCR + tok + str.charAt(startIndex));                            
                        }
                    } catch (StringIndexOutOfBoundsException sioobe) {
                        result.append(NCR + tok);
                    }
                }

                if (NCR != "\\u" && NCR != "U+") {
                    startIndex++;
                }
            }

            str = result.toString();
            result.setLength(0);
        }
        return str;
	}
	public static String removeTagA(String str)
	{
		str = str.replaceAll("<a[^>]*>", "").replaceAll("</[^>]*>", "");
		return str;
	}
}
